package com.xxd.spring.boot.demo.wubolin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxd.spring.boot.demo.wubolin.entity.User;
import com.xxd.spring.boot.demo.wubolin.exception.MyException;
import com.xxd.spring.boot.demo.wubolin.mapper.UserMapper;
import com.xxd.spring.boot.demo.wubolin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int regsit(User user) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",user.getUsername());
        User dbUser = userMapper.selectOne(queryWrapper);
        if(dbUser != null){
            throw new MyException("用户已存在","123");
        }
        return userMapper.insert(user);
    }

    @Override
    public User login(String username, String passward) throws MyException {
        User user = userMapper.selectByUsernamePassward(username,passward);
        if(user == null){
            throw new MyException("用户名或密码错误","115");
        }
        return user;
    }
}
